package main

import (
	"bufio"
	"fmt"
	"github.com/eclipse/paho.mqtt.golang"
	"github.com/gitstliu/go-redis-cluster"
	"io"
	"io/ioutil"
	"net"
	"net/http"
	"os"
	"strconv"
	"strings"
	"time"
)

var s = true

func main() {
	test()
	//
	//return
	// 关闭
	//go aiui(1, "605394647632969758.pcm")
	// 打开
	//go aiui(2, "5577006791947779410.pcm")
	//go aiui(3)
	//go aiui(4)
	//go aiui(5)
	//go aiui(6)
	//go aiui(7)
	//go aiui(8)
	//go aiui(9)
	//fmt.Println("success")
	//ch := make(chan int)
	//<-ch
}

func test() {

	cluster, err := redis.NewCluster(
		&redis.Options{
			StartNodes:   []string{"119.23.182.1:6379"},
			ConnTimeout:  50 * time.Millisecond,
			ReadTimeout:  50 * time.Millisecond,
			WriteTimeout: 50 * time.Millisecond,
			KeepAlive:    16,
			AliveTime:    60 * time.Second,
		})
	fmt.Println(cluster, err)

	//b := mqtt2.IsOnline("dc4f2252fd35")
	//
	//fmt.Println(b)

	//body, err := httplib.
	//	Get("http://mqtt.rcenet.com:9503/api/v3/connections/dc4f2252fd35").
	//	SetBasicAuth("189ba64e2ec9c", "Mjg2OTY3ODcyNTUzMjAwODg5NjIzMTg5NzM4Mzk5MzM0NDA").
	//	String()
	//
	//fmt.Println(body, err)

	//fmt.Println(get_external())
	//err := tools.NewJpush().SetAlias("5b0e6847c175f42e75484e53").SetAlert("555555555555").Jump("message").Send()
	//fmt.Println(err)
	//res, err := xfyun.NewAiui().
	//	SetAuthId("444444445555").
	//	SetUserParams(map[string]interface{}{
	//		"user_id":     "1233333",
	//		"sta":         "1234",
	//		"device_type": 1,
	//	}).
	//	SetScene("main").
	//	Text("今天天气怎么样")
	//
	//fmt.Println(res, err)
}

func get_external() string {
	resp, err := http.Get("http://myexternalip.com/raw")
	if err != nil {
		return ""
	}
	defer resp.Body.Close()
	content, _ := ioutil.ReadAll(resp.Body)
	//buf := new(bytes.Buffer)
	//buf.ReadFrom(resp.Body)
	//s := buf.String()
	return string(content)
}

func GetPulicIP() string {
	conn, _ := net.Dial("udp", "8.8.8.8:80")
	defer conn.Close()
	localAddr := conn.LocalAddr().String()
	idx := strings.LastIndex(localAddr, ":")
	return localAddr[0:idx]
}

func aiui(n int, fname string) {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("err:", err)
		}
	}()
	cid := fmt.Sprintf("123456-%d", n)

	ops := mqtt.
		NewClientOptions().
		AddBroker("tcp://www.rcenet.com:9505").
		SetUsername("server").
		SetPassword("123456").
		SetClientID(cid).
		SetKeepAlive(15 * time.Second).
		SetPingTimeout(5 * time.Second).
		SetProtocolVersion(4)

	client := mqtt.NewClient(ops)

	if token := client.Connect(); token.Wait() && token.Error() != nil {
		fmt.Println(cid, "connect fail")
		return
	}

	client.Publish("service/dc4f2252fd7a/say/0", 0, false, "begin")
	i := 0
	ReadBlock(fname, 2*1024, func(bytes []byte) {
		i++
		client.Publish("service/dc4f2252fd7a/say/"+strconv.Itoa(i), 0, false, string(bytes))

		//<-time.After(400 * time.Millisecond)
	})
	client.Publish("service/dc4f2252fd7a/say/"+strconv.Itoa(i+1), 0, false, "end")

	return

	if s {
		goto A
	} else {
		goto B
	}

A:
	for {
		client.Publish("service/123456/say/0", 0, false, "begin")
		i := 0
		ReadBlock("1.pcm", 2*1024, func(bytes []byte) {
			i++
			client.Publish("service/123456/say/"+strconv.Itoa(i), 0, false, string(bytes))

			//<-time.After(400 * time.Millisecond)
		})
		client.Publish("service/123456/say/"+strconv.Itoa(i+1), 0, false, "end")
		<-time.After(1 * time.Second)
		//break
	}
	return
B:
	client.Publish("service/123456/say/0", 2, false, "begin")
	<-time.After(20)
	client.Publish("service/123456/say/1", 2, false, "a")
	<-time.After(20)
	client.Publish("service/123456/say/2", 2, false, "b")
	<-time.After(20)
	client.Publish("service/123456/say/3", 2, false, "c")
	<-time.After(20)
	client.Publish("service/123456/say/4", 2, false, "d")
	<-time.After(20)
	client.Publish("service/123456/say/5", 2, false, "e")
	<-time.After(20)
	client.Publish("service/123456/say/6", 2, false, "f")
	<-time.After(20)
	client.Publish("service/123456/say/7", 2, false, "g")
	<-time.After(20)
	client.Publish("service/123456/say/8", 2, false, "end")

	return
}

func ReadBlock(filePth string, bufSize int, hookfn func([]byte)) error {
	f, err := os.Open(filePth)
	if err != nil {
		return err
	}
	defer f.Close()

	buf := make([]byte, bufSize) //一次读取多少个字节
	bfRd := bufio.NewReader(f)
	for {
		n, err := bfRd.Read(buf)
		hookfn(buf[:n]) // n 是成功读取字节数

		if err != nil { //遇到任何错误立即返回，并忽略 EOF 错误信息
			if err == io.EOF {
				return nil
			}
			return err
		}
	}

	return nil
}
